﻿using System;
using System.Drawing;

namespace PlantsZombies.Common.Zombie
{
    public unsafe class ZombieObject : GameObject
    {
        protected int _moveSpacing = 1;
        public ZombieObject(ImageObject[] images,SpeciesType speciesType, int life,int damage, int moveSpacing, Size size)
            : base(speciesType, life, size)
        {
            base.SetImage(images);
            this.Damage = damage;
            _moveSpacing = moveSpacing;
        }
        public event Action AttackPlantCallback;
        public override void Attack(ListGameObject* ptr)
        {
            // 僵尸走到头了
            if (Location.X <= StartX)
            {
                if (CompletionCallback != null)
                {
                    CompletionCallback();
                    return;
                }
            }
            // 判断僵尸攻击植物
            if (Column > 0)
            {
                ListGameObject* p = ptr + Row * MaxColumn + (Column - 1);
                for (int k = 0; k < p->Game.Count; k++)
                {
                    var game = p->Game[k];
                    // 如果僵尸前面是植物，并且间距小于等于默认间距，则开始攻击植物
                    if (!game.IsZombie() && this.Location.X - (game.Location.X + (game.Size.Width >> 1)) <= spacing && _curPlayIndex == _images.Length - 1)
                    {
                        game.Life -= Damage;
                        if (AttackPlantCallback != null)
                            AttackPlantCallback();
                    }
                }
            }
        }
        public override void Move(ListGameObject* ptr)
        {
            if (Column > 0)
            {
                ListGameObject* p = ptr + Row * MaxColumn + (Column - 1);
                if (p->Game.Count > 0)
                {
                    for (int k = 0; k < p->Game.Count; k++)
                    {
                        var game = p->Game[k];
                        // 如果僵尸前面是植物，并且植物的生命值大于零，则僵尸不能往前移动
                        if (!game.IsZombie() && this.Location.X - (game.Location.X + (game.Size.Width >> 1)) <= spacing && game.Life > 0) return;
                    }
                }
            }
            var x = this.Location.X - _moveSpacing;
            if (x < StartX)
                x = StartX;
            var tempCol = (x - StartX) / (float)CellWidth;
            int col = 0;
            col = (int)Math.Ceiling(tempCol) - 1;

            // 更新位置
            this.Location = new System.Drawing.Point(x, this.Location.Y);
            ListGameObject* temp = ptr + Row * MaxColumn + col;
            if (col != base.Column)
            {
                if (temp->Game.Count > 0)
                {
                    int count = 0;
                    for (int k = 0; k < temp->Game.Count; k++)
                    {
                        var game = temp->Game[k];
                        if (!game.IsZombie())
                        {
                            if (game.Life <= 0)// 被吃掉了
                            {
                                temp->Game.RemoveAt(k);
                                Update(ptr, temp, col);
                            }
                        }
                        else
                            count++;
                        if (count == temp->Game.Count)
                            Update(ptr, temp, col);
                    }
                }
                else
                    Update(ptr, temp, col);
            }
        }
        private void Update(ListGameObject* ptr, ListGameObject* temp, int col)
        {
            ListGameObject* lastP = ptr + Row * MaxColumn + Column;
            temp->Game.Add(this);
            lastP->Game.Remove(this);
            base.Column = col;
        }
        const int spacing = 5;// 默认植物与僵尸的间距小于等于3
    }
}
